package househomework;

import java.util.Scanner;

public class HouseTable {
  HouseService hs =new HouseService();
  Scanner scan =new Scanner(System.in);

    public boolean add(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入新的房屋信息");
        System.out.println("请输入房主名字");

        String houseName = scan.next();
        System.out.println("请输入电话号码");
        String housePhone = scan.next();
        System.out.println("请输入地址");
        String houseAddress = scan.next();
        System.out.println("请输入月租");
        double monthPrice = scan.nextDouble();
        System.out.println("目前出租状态：输入true或者false");
        String trueOrFalse =scan.next();
        boolean state="true".equalsIgnoreCase(trueOrFalse);
        House house= new House(houseName,housePhone,houseAddress,monthPrice,state);

        if (hs.add(house)){
            System.out.println("------------添加房屋------------\n" +
                    "姓名：" +house.getHouseName()+"\n"+
                    "电话：" +house.getHousePhone()+"\n"+
                    "地址：" +house.getHouseAddress()+"\n"+
                    "月租：" +house.getMonthPrice()+"\n"+
                    "状态(未出租/已出租):"+house.isState()+
                    "\n------------添加完毕------------");
            return true;
        }

        return false;

    }//添加房屋

    public void findTheHouse(House[] houses){

        System.out.println("------------查找房屋------------");
        System.out.println("请输入你要查找的id：");
        Scanner scan =new Scanner(System.in);
        int id =scan.nextInt();
        System.out.println("编号"+"\t\t"+"房主"+"\t\t"+"电话"+"\t\t"+"地址"+"\t\t"+"月租"+"\t\t"+"出租/未出租");

        if (hs.findHouse(id)!=null){
            System.out.println( hs.findHouse(id));
        }else {
            System.out.println("您输入的id暂时没有房源信息");
        }


    }//查找房屋

    public void deleteTheHouse(){
        System.out.println("------------删除房屋------------");
        System.out.println("请选择需要删除的房屋编号：（-1退出）");
        int id =scan.nextInt();
        if (id==-1){
            flag=false;
            return;
        }

        if (hs.deleteHouse(id)){
            System.out.println("删除成功");
        }else {
            System.out.println("删除编号不存在或者删除失败");
        }
    }//删除房屋

    public void changeTheHouse(){
        System.out.println("------------修改房屋------------");
        System.out.println("请选择需要修改的房屋编号：（-1退出）");
        Scanner scan =new Scanner(System.in);
        int houseId =scan.nextInt();
        if (houseId==-1){
            flag=false;
            return;
        }
        House houses=hs.findHouse(houseId);
        if (houses!=null){
            System.out.println("请输入新的房屋信息");
            System.out.println("请输入房主名字");
            String upName=houses.getHouseName();
            houses.setHouseName(scan.next());

            System.out.println("请输入电话号码");
            String upPhone=houses.getHousePhone();
            houses.setHousePhone(scan.next());

            System.out.println("请输入地址");
            String upAddress=houses.getHouseAddress();
            houses.setHouseAddress(scan.next());

            System.out.println("请输入月租");
            double upPrice=houses.getMonthPrice();
            houses.setMonthPrice(scan.nextDouble());

            System.out.println("目前出租状态：输入true或者false");
            boolean upState=houses.isState();
            houses.setState("true".equalsIgnoreCase(scan.next()));

            System.out.println("------------更改完成------------\n" +
                    "姓名("+upName+")：" +houses.getHouseName()+"\n"+
                    "电话("+upPhone+")：" +houses.getHousePhone()+"\n"+
                    "地址("+upAddress+")：" +houses.getHouseAddress()+"\n"+
                    "月租("+upPrice+")：" +houses.getMonthPrice()+"\n"+
                    "状态(未出租/已出租)("+upState+"):"+houses.isState()+
                    "\n------------更改完毕------------");
        }else {
            System.out.println("没有房屋信息可以修改");
        }

    }//修改房屋信息

    public void HousingList(House[] houses){

        System.out.println("------------房屋列表------------");
        System.out.println("编号"+"\t\t"+"房主"+"\t\t\t"+"电话"+"\t\t\t\t\t"+"地址"+"\t\t\t\t"+"月租"+"\t\t\t\t"+"出租/未出租");

        for (int i=0;i< houses.length;i++){
            if (houses[i]!=null) System.out.println(houses[i]);

        }
    }//查看房屋列表

    public void exit(){

        System.out.println("------------退出系统------------");
        System.out.println("请输入你的选择(Y/N)");
        String TorF = scan.next();
        if ("Y".equalsIgnoreCase(TorF)){
            System.out.println("你退出了程序~~");
            flag=false;

        }else if ("N".equalsIgnoreCase(TorF)){
            System.out.println("退出失败");
        }else {
            System.out.println("你的输入有误，返回上一层重新输入");
        }

    }//退出系统

    boolean flag=true;
    public void mainMenu(){//展示菜单

        do {
            System.out.println("------------房屋出租系统------------" +
                    "\n\t\t1 新 增 房 源" +
                    "\n\t\t2 查 找 房 屋" +
                    "\n\t\t3 删 除 房 屋" +
                    "\n\t\t4 修 改 房 屋 信 息" +
                    "\n\t\t5 房 屋 列 表" +
                    "\n\t\t6 退       出");

            Scanner scan =new Scanner(System.in);
            int input = scan.nextInt();
            switch (input){
                case 1:{
                    add();
                    break;
                }
                case 2:{
                    findTheHouse(hs.houses);
                    break;
                }
                case 3:{
                    deleteTheHouse();
                    break;

                }
                case 4:{
                    changeTheHouse();
                    break;
                }
                case 5:{
                    HousingList(hs.checkHouse());
                    break;
                }
                case 6:{
                    exit();
                    break;
                }
                default:
                    System.out.println("你的输入有误，请重新输入");

            }
        } while (flag);
    }
}
